<?php

/**
 * This is the model class for table "{{ord_product}}".
 *
 * The followings are the available columns in table '{{ord_product}}':
 * @property integer $zop_id
 * @property string $zo_id
 * @property integer $zp_id
 * @property integer $zm_id
 * @property string $zop_title
 * @property integer $zop_meal
 * @property integer $zop_nums
 * @property double $zop_price
 * @property double $zop_real_price
 * @property integer $zop_weight
 * @property string $zop_spe
 * @property string $zop_pic
 * @property integer $zop_point
 * @property integer $zop_set_point
 */
class OrdProduct extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return OrdProduct the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{ord_product}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zo_id, zp_id, zm_id, zop_title', 'required'),
			array('zp_id, zm_id, zop_meal, zop_nums, zop_weight, zop_point, zop_set_point', 'numerical', 'integerOnly'=>true),
			array('zop_price, zop_real_price', 'numerical'),
			array('zo_id', 'length', 'max'=>20),
			array('zop_title', 'length', 'max'=>100),
			array('zop_pic', 'length', 'max'=>255),
			array('zop_spe,zpa_pro_no', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zop_id, zo_id, zp_id, zpa_pro_no, zm_id_mem ,zm_id, zop_title, zop_meal, zop_nums, zop_price, zop_real_price, zop_weight, zop_spe, zop_pic, zop_point, zop_set_point', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'product'=>array(self::BELONGS_TO, 'Product', 'zp_id'),//产品
			'member'=>array(self::BELONGS_TO, 'Member', 'zm_id'),//商家
            'order'=>array(self::BELONGS_TO, 'Order', 'zo_id'),
			'comment'=>array(self::HAS_ONE, 'OrdComment', 'zop_id','on'=>'comment.zoc_totype = 2'),	//订单商品评论
		);
	}

	/**
	 * @根据订单id返回订单商品记录.
	 */
	public function getOrderProduct($orderid)
	{
		$psql = 'select * from {{ord_product}} where zo_id='.$orderid.' ORDER BY zop_id DESC';
		$productArray = Yii::app()->db->createCommand($psql)->queryAll();
		return $productArray;
	}

	/**
	 * @根据订单id返回第一个订单商品.
	 */
	public function getOrderFirstProduct($orderid)
	{
		$psql = 'select * from {{ord_product}} where zo_id='.$orderid.' ORDER BY zop_id DESC';
		$product = Yii::app()->db->createCommand($psql)->queryRow();
		return $product;
	}

	/**
	 * @根据订单商品id返会最大退款额
	 * $id 订单商品表id，非商品id
	 * $memberid 订单的会员id
	 * $check 是否检查能否退款
     * boolen $throw 是否抛出错误
	 */	
	public function calculateForRefund($id, $memberid=null, $check=null, $throw=null){
		if($check){
			$amount = $this->checkForRefund($id, $memberid);
		}		
		if( !$check || $amount>0 ){
			//执行计算，待完善
			$record = $this->findByPk($id);
			$amount = $record->zop_real_price;
		}
		if($throw && $amount<=0){
			if($amount == -2){
				Message('订单商品不存在！');				
			}elseif($amount == -3){
				Message('该商品已过申请退款时间，请通过申请售后进行退款！');
			}elseif($amount == -1){
				Message('该商品已经申请退款，不能再申请！');
			}elseif($amount == 0){
				Message('该商品可退款额为0，不能申请退款，详情请查看退款计算规则！');
			}
		}
		return $amount;
	}

	/**
	 * @根据订单商品id判断是否可以退款，若不可退款，返回-1，若订单商品不存在，返回-2
	 * $id 订单商品表id，非商品id
	 */	
	public function checkForRefund($id=null, $memberid=null){
		//1.检查订单商品是否存在，是否该用户的 2.检查商品是否已申请退款 3.检查订单商品是否对应用户id
		$check = 1;
		$record = $id ? $this->findByPk($id) : $this;
		$order = Order::model()->findByPk($record->zo_id);

		//读取配置缓存
		$params = Yii::app()->filecache->get('setting');
		//print_r($params);exit;
		$time = isset($params['refunddays']) ? $params['refunddays']*24*3600 : 7*24*3600;
		$past = time()-$order->zo_voer_time-$time;
		if(!$record || !$record->zop_nums){
			$check = -2;
		}elseif( $past<0 ){
			$check = -3;
		}elseif($record->zop_status){
			$check = -1;
		}
		if($check>0 && $memberid){
			$check = $record->order->zm_id==$memberid ? 1 : -2;
		}
		return $check;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zop_id' => 'Zop',
			'zo_id' => 'Zo',
			'zp_id' => 'Zp',
			'zm_id' => 'Zm',
			'zop_title' => 'Zop Title',
			'zop_meal' => 'Zop Meal',
			'zop_nums' => 'Zop Nums',
			'zop_price' => 'Zop Price',
			'zop_real_price' => 'Zop Real Price',
			'zop_weight' => 'Zop Weight',
			'zop_spe' => 'Zop Spe',
			'zpa_pro_no'=>'商家编号',
			'zop_pic' => 'Zop Pic',
			'zop_point' => 'Zop Point',
			'zop_set_point' => 'Zop Set Point',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zop_id',$this->zop_id);
		$criteria->compare('zo_id',$this->zo_id,true);
		$criteria->compare('zp_id',$this->zp_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zop_title',$this->zop_title,true);
		$criteria->compare('zop_meal',$this->zop_meal);
		$criteria->compare('zop_nums',$this->zop_nums);
		$criteria->compare('zop_price',$this->zop_price);
		$criteria->compare('zop_real_price',$this->zop_real_price);
		$criteria->compare('zop_weight',$this->zop_weight);
		$criteria->compare('zop_spe',$this->zop_spe,true);
		$criteria->compare('zpa_pro_no',$this->zpa_pro_no,true);
		$criteria->compare('zop_pic',$this->zop_pic,true);
		$criteria->compare('zop_point',$this->zop_point);
		$criteria->compare('zop_set_point',$this->zop_set_point);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}